import * as CartRequest from "~/assets/cart/cart";

export const createOrder = (vm) => {
  let list = []
  vm.tableData.forEach(item => {
    let ob = {
      commodity: item.commodity.id,
      count: item.count,
      id: 0,
      orderId: 0,
      price: item.commodity.price,
      total: item.total
    }
    list.push(ob)
  })

  let data = {
    address: vm.addressDate.address,
    createTime: "",
    message: vm.message,
    mobile: vm.addressDate.mobile,
    name: vm.addressDate.name,
    orderItems: list,
    province: JSON.stringify(vm.addressDate.province),
    status: "已支付",
    suserId: vm.$store.state.userInfo.id,
    total: vm.totalMoney

  }
  vm.$axios.post('/order', data)
    .then((res) => {
      console.log(res)
      vm.$message.success("付款成功")
      vm.tableData.forEach(item => {
        deleteCartInfo(vm, item.id);
      })
    })
}
export const deleteCartInfo = (vm, cartItemId) => {
  let data = cartItemId
  vm.$axios.delete('/cartItem/cartItemId?cartItemId=' + data)
    .then((res) => {
      CartRequest.getCartInfo(vm);
    })
}

export const getOrderList = (vm) => {
  let data = {
    userId: vm.$store.state.userInfo.id
  }
  vm.$axios.get('/order/select/userId', {params: data})
    .then((res) => {
      console.log(res)
      vm.tableData = res.data.data
      vm.isFlag = res.data.data.length
    })
}

